<?php
/**
 * 子应用入口
 * @param  array
 * @return [type]
 */
function run_page_app($uri = array()) {
	$request_uri = str_replace('/index.php', '', $_SERVER['REQUEST_URI']);
	page_show_page($request_uri);
}

/**
 * 统计并显示统计数据
 * @param  string
 * @return [type]
 */
function page_show_count($url = '') {
	if (empty($url)) {
		return;
	}
	$url = filter_data($url);
	$ip = get_ip();
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	require_once APP_PATH . '/page/model.php';
	$ret = page_model_insert_count($url, $ip, $user_agent);
	if (!$ret) {
		//同一个IP不允许重复记录
		//更新数据
		page_model_update_count_by_url($url);
	}
	//获取数据
	$page_view_count = page_model_get_count_by_url($url);

	//显示数据
	echo 'document.write("' . $page_view_count . '");';
}

/**
 * 显示page页面
 * @param  string
 * @return [type]
 */
function page_show_page($url = '') {
	if (empty($url)) {
		show_not_found();
		return;
	}
	$url = filter_data($url);
	// app start.
	if (is_file(ROOT_PATH . $url . 'index.html') && (time() - filemtime(ROOT_PATH . $url . 'index.html') < 1200) && !IS_DEBUG) {
		//缓存未失效则直接加载静态文件
		include ROOT_PATH . $url . 'index.html';
	} else {
		require_once APP_PATH . '/page/build.php';
		$page_html_path = page_build_page_by_url($url);
		//输出
		if ($page_html_path) {
			include $page_html_path;
		} else {
			show_not_found();
		}
	}
}